
package com.jf.cloud.auth.feign;

import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.jf.cloud.api.auth.feign.AuthSocialFeignClient;
import com.jf.cloud.api.auth.vo.AuthSocialVO;
import com.jf.cloud.auth.config.WxConfig;
import com.jf.cloud.auth.mapper.AuthSocialMapper;
import com.jf.cloud.auth.model.AuthSocial;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import io.swagger.v3.oas.annotations.Hidden;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author zz
 * @date 2020/9/22
 */
@RestController
@Hidden
public class AuthSocialController implements AuthSocialFeignClient {

    @Autowired
    private AuthSocialMapper authSocialMapper;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private WxConfig wxConfig;


    @Override
    public ServerResponseEntity<AuthSocialVO> getByTempUid(String tempUid) {
        AuthSocial authSocial = authSocialMapper.getByTempUid(tempUid);
        return ServerResponseEntity.success(mapperFacade.map(authSocial, AuthSocialVO.class));
    }

    @Override
    public ServerResponseEntity<String> getMobileByMaInfo(String sessionKey, String encryptedData, String ivStr) {
        WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxConfig.getWxMaService().getUserService().getPhoneNoInfo(
                sessionKey, encryptedData, ivStr);
        return ServerResponseEntity.success(wxMaPhoneNumberInfo.getPhoneNumber());
    }

    @Override
    public ServerResponseEntity<AuthSocialVO> getByUserIdAndSocialType(String bizUserId, Integer socialType) {
        AuthSocial authSocial = authSocialMapper.getByBizUserIdAndType(bizUserId, socialType);
        return ServerResponseEntity.success(mapperFacade.map(authSocial, AuthSocialVO.class));
    }

    @Override
    public ServerResponseEntity<AuthSocialVO> getBizUserIdByPhone(String phone) {
        AuthSocial authSocial = authSocialMapper.getBizUserIdByPhone(phone);
        return ServerResponseEntity.success(mapperFacade.map(authSocial, AuthSocialVO.class));
    }

    @Override
    public ServerResponseEntity<AuthSocialVO> getUserInfoByUserId(Long userId) {
        AuthSocial authSocial = authSocialMapper.getUserInfoByUserId(userId);
        return ServerResponseEntity.success(mapperFacade.map(authSocial, AuthSocialVO.class));
    }

}
